﻿using System;
using BKSystem.IO;

namespace Lazeeboy.FantomG
{
    class LiveSet
    {
        public const int LIVESET_LENGTH = 0x89C;
        public const int LIVESET_START = 0x100;
        public const int LIVESET_NAME_OFFSET = 0x0;
        public const int LIVESET_RPSSET_OFFSET = 0x88F;

        private RPSSet _rpsSet;
        private static LiveSet[] _liveSets;
        public static LiveSet[] LiveSets
        { get { return _liveSets; } }

        private int _id;
        protected string _name;

        static LiveSet()
        {
            _liveSets = new LiveSet[512];
        }

        public LiveSet(int ID, string Name)
        {
            _id = ID;
            
            if(Name != null)
                _name = Name.Trim();
        }

        public LiveSet(int ID, string Name, RPSSet RPSSet)
            : this(ID, Name)
        {
            _rpsSet = RPSSet;
        }

        public int ID
        { 
            get { return _id; } 
            set { _id = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public bool IsDefault
        { get { return Name == DefaultName; } }

        public string DefaultName
        { get { return "INIT LIVE SET"; } }

        public RPSSet RPS
        { get { return _rpsSet; } }

        public override string ToString()
        {
            return string.Format("{0:000}: {1} RPS Set:{2}", ID, Name, _rpsSet);
        }

        public static LiveSet Lookup(int ID)
        {
            LiveSet Result = null;

            foreach (LiveSet liveSet in _liveSets)
                if (liveSet != null && liveSet.ID == ID)
                    return liveSet;

            return Result;
        }

        public static LiveSet AddLiveSetWithRPS(int ID, string Name, short RPS)
        {
            LiveSet liveSet = null;

            if (Lookup(ID) == null)
            {
                liveSet = new LiveSet(ID, Name, RPSSet.FindByPosition(RPS));
                _liveSets[ID-1] = liveSet;
            }

            return liveSet;
        }

        public bool Load(BitStream BitStream, RPSSet[] RPSSets)
        {
            bool Result = false;

            if (BitStream != null && BitStream.CanRead && BitStream.CanSeek)
            {
                BitStream.Position = ((long)LIVESET_START + LIVESET_LENGTH + LIVESET_NAME_OFFSET) * ID * 8;

                byte sevenBits;
                char asciiChar;
                _name = "";

                for (int charactorPosition = 0; charactorPosition < 16; charactorPosition++)
                {
                    BitStream.Read(out sevenBits, 0, 7);
                    asciiChar = (char)sevenBits;

                    if (!Char.IsControl(asciiChar))
                        _name += asciiChar;
                    else
                        throw new Exception("LiveSet's Name contains control characters");
                }

                BitStream.Position = ((long)LIVESET_START + LIVESET_LENGTH + LIVESET_RPSSET_OFFSET) * ID * 8;

                _rpsSet = RPSSet.FindByPosition((short)(BitStream.ReadByte() + 1));
            }

            return Result;
        }
    }
}
